<%@page language="java"  import="com.scalar.chiptrack.utils.ConfigManager,
                                 com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm,
                                 com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem,
                                 com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry,
                                 com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntryList" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/purchaseOrders.do"/>

<%
    String chipTracContext = "/" + ConfigManager.getChipTracContext();
    int purchaseOrderLineItemNumber = -1;
    try{
        purchaseOrderLineItemNumber = Integer.parseInt( request.getParameter("purchaseOrderLineItemNumber") );
    }catch(NumberFormatException nfe){};
%>

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script language="javascript">

function addPurchaseOrderLineItemLotDataEntry( purchaseOrderLineItemNumber )
{
    document.purchaseOrderForm.action='<%=chipTracContext%>/purchaseOrders.do?action=purchaseOrderLotEntryAddMore&purchaseOrderLineItemNumber=' + purchaseOrderLineItemNumber;
    document.purchaseOrderForm.submit();
}

function SaveAndClose()
{
    var totalQty = 0;
    var lineItemLotObj = document.purchaseOrderForm.lineItemLot;
    var tmpErrMsg = '';
    var errorIndex = document.getElementById("errorIndex");
    if( typeof lineItemLotObj.length == 'undefined' )
    {
        var qtyIn = parseInt ( document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[0].qtyIn'].value);
        var qtyOut = parseInt ( document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[0].qtyOut'].value);
        var lotNumber = document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[0].lotNumber'].value;
        if( isNaN( qtyIn ) )
        {
            qtyIn = 0;
        }
        if( isNaN( qtyOut ) )
        {
            qtyOut = 0;
        }

        if ( lotNumber == '' && qtyIn == 0 && qtyOut == 0 )
        {
            errorIndex.innerHTML = "<li>Please enter valid data";
            errorIndex.focus();
            return;
        }
        tmpErrMsg = validate( lotNumber, qtyIn, qtyOut );
        if( tmpErrMsg != '' )
        {
            errorIndex.innerHTML = tmpErrMsg;
            errorIndex.focus();
            return;
        }
        totalQty    = parseInt ( document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[0].qtyIn'].value );
    }
    else
    {
        var rowCnt = 0;
        var i;
        for ( i = 0; i < lineItemLotObj.length; i++ )
        {
            var qtyIn = parseInt ( document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[' + i + '].qtyIn'].value);
            var qtyOut = parseInt ( document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[' + i + '].qtyOut'].value);
            var lotNumber = document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1%>].purchaseOrderLineItemLotDataEntryList[' + i + '].lotNumber'].value;
            if( isNaN( qtyIn ) )
            {
                qtyIn = 0;
            }
            if( isNaN( qtyOut ) )
            {
                qtyOut = 0;
            }
            if ( lotNumber == '' && qtyIn == 0 && qtyOut == 0 )
            {
                rowCnt ++;
                continue;
            }

            tmpErrMsg = validate( lotNumber, qtyIn, qtyOut );
            if( tmpErrMsg != '' )
            {
                errorIndex.innerHTML = tmpErrMsg + " for row "+eval(i+1);
                errorIndex.focus();
                return;
            }
            totalQty  = totalQty +  parseInt ( document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber-1 %>].purchaseOrderLineItemLotDataEntryList[' + i + '].qtyIn'].value );
        }

        if( rowCnt == i )
        {
            errorIndex.innerHTML = "<li>Atleast one row should contain valid data";
            errorIndex.focus();
            return;
        }
    }
    if ( isNaN( totalQty ) )
    {
        totalQty = 0;
    }
    var requestedQty = parseInt( window.opener.document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber -1 %>].requestedQuantity'].value );
    if( isNaN( requestedQty ))
    {
        requestedQty = 0;
    }

    if( totalQty <= requestedQty )
    {
        window.opener.document.purchaseOrderForm['purchaseOrderLineItemList[<%= purchaseOrderLineItemNumber -1 %>].shippedQty'].value = totalQty;
    }
    else
    {
        errorIndex.innerHTML = "<li>Total Quantity-In must be less than or equal to "+requestedQty;
        errorIndex.focus();
        return;
    }

    if( totalQty > 0 )
    {
        window.opener.disableHolds(<%= purchaseOrderLineItemNumber -1 %>, true )
    }
    document.purchaseOrderForm.action='<%=chipTracContext%>/purchaseOrders.do?action=purchaseOrderLineItemLotEntryClose';
    document.purchaseOrderForm.submit();
}

function validate( lotNumber, qtyIn, qtyOut )
{
    if( lotNumber != '' )
    {
        if ( qtyIn == 0 )
        {
          return "<li>Enter valid Quantity-In"
        }
        if ( qtyIn < qtyOut )
        {
            return "<li>Quantity-In cannot be less than Quantity-Out"
        }
    }
    else if( qtyIn != 0 )
    {
        if( lotNumber == '' )
        {
            return "<li>Lot Number cannot be blank if Quantity-In is entered"
        }
        if ( qtyIn < qtyOut )
        {
            return "<li>Quantity-In cannot be less than Quantity-Out"
        }
    }
    else if( qtyOut != 0 )
    {
        return "<li>Please enter Lot Number and Quantity-In"
    }
    return "";
}
</script>
</HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
	    <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
        <TD bgcolor="#ffffff">
            <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
        <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
        <TD valign="top" width="100%">
            <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
            <BR>
<!-- from here your body starts -->
<html:form name="purchaseOrderForm"  type="com.scalar.chiptrack.finance.openpo.forms.purchaseOrderForm" action="/purchaseOrders.do">

             <table border="0" align = "left" width ="100%" >
             <TR>
                    <TD WIDTH="100%">
                        <TABLE BORDER="0" WIDTH="100%" HEIGHT="175%">
                            <TR>
                                <TD WIDTH="100%" align = "">
                                    <table width="75%" ALIGN="CENTER"><tr><td><center><FONT id="poErrors" styleClass="NArialL"><b>Purchase Order Line Item Lot Data Entry</b></FONT></center></td></TR></table>
                                </TD>
                            </TR>
                        </TABLE>
                        <BR>
                        <TABLE BORDER="0" ALIGN="CENTER" WIDTH="75%" CLASS="NArialL">
                            <tr>
                                <td colspan='3' class='NArialL' style='color:red' id="errorIndex"></td>
                            </tr>
                            <tr>
                                <td colspan='3'></td>
                            </tr>
                            <TR>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Lot Number&nbsp;*</FONT>
                                </TD>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Quantity In&nbsp;*</FONT>
                                </TD>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">Quantity Out&nbsp;</FONT>
                                </TD>
                            </TR>
                            <nested:iterate id="purchaseOrderLineItem" property="purchaseOrderLineItemList" type="com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem">
                                <nested:equal name="purchaseOrderLineItem" property = "purchaseOrderLineItemNumber"  value = "<%= String.valueOf( purchaseOrderLineItemNumber )%>" >
                                    <nested:hidden property="purchaseOrderLineItemNumber"/>
                                    <nested:iterate id = "purchaseOrderLineItemLotDataEntry" property="purchaseOrderLineItemLotDataEntryList" type="com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry">
                                    <TR>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <input type="hidden" name="lineItemLot">
                                            <P><nested:text property="lotNumber" styleClass="txfield" size="19" maxlength="50"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="qtyIn" styleClass="valuetxfield" size="19" onkeypress="javascript:allowInteger()" maxlength="6"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="qtyOut" styleClass="valuetxfield" size="19" onkeypress="javascript:allowInteger()" maxlength="6"/>
                                            </CENTER>
                                        </TD>
                                    </TR>
                                    </nested:iterate>
                                </nested:equal>
                            </nested:iterate>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR align = "center" border="0">
                                <TD  bgColor= "" align="right" colspan = '3'>
                                <% String jsMethod = "javascript:addPurchaseOrderLineItemLotDataEntry('" + purchaseOrderLineItemNumber + "')" ; %>
                                <A href="<%=jsMethod%>"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" onmouseover="window.status='Click to Create PO';return true" onmouseout="window.status='';return true" title="Click here to Create PO"></A>&nbsp;&nbsp;&nbsp;

                                </TD>
                            </TR>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR align = "center" border="0">
                                <TD  bgColor= "" align="center" colspan = '3'>
                                <A href="javascript:SaveAndClose()" ><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onmouseover="window.status='Click to Create PO';return true" onmouseout="window.status='';return true" title="Click here to Create PO"></A>&nbsp;&nbsp;&nbsp;
                                <A href="javascript:window.close()" onMouseOver="window.status='Click to Reset the PO Information';return true" onMouseOut="window.status='';return true" title="Click here to Reset Process Information"><IMG border=0 src="<%=chipTracContext%>/jsp/includes/images/cancel.gif"></a>
                                </TD>
                            </TR>
                        </TABLE>
                        <BR>
              </TABLE>
</html:form>
        <BR><BR><BR>
        </TD>
        <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
    </TR>
</TABLE>
</BODY>
</HTML>
